#define _WIN32_WINNT 0x0501
#include "moeutil/net/async_archive_socket.h"


class MyObject
{
public:
	MyObject()
		: mText("MyObjectText"), mNumber(100)
	{
	}
	template<typename Archive>
	void serialise(Archive& ar)
	{
		ar & mText;
		ar & mNumber;
	}

public:
	std::string mText;
	int mNumber;
};



std::vector<std::string*> g_Texts;

typedef moeutil::net::async_archive_socket<tcpsocket> wrap_tcpsocket;


void on_send(wrap_tcpsocket& sock)
{
	std::cout<<"send complete......"<<std::endl;
}

void on_send_exception(wrap_tcpsocket& sock, const moeutil::net::socket_errcode& ec)
{
	std::cout<<ec.message()<<std::endl;
}

void main()
{
	std::string s1 = "Text1";
	std::string s2 = "Text2";
	std::string s3 = "Text3";

	MyObject obj;

	g_Texts.push_back(&s1);
	g_Texts.push_back(&s2);
	g_Texts.push_back(&s3);

	boost::asio::io_service ioserv;
	tcpsocket_ptr sock(new tcpsocket(ioserv));

	tcp_endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 4001);
	sock->connect(ep);

	::Sleep(500);
	wrap_tcpsocket wrapSock(sock);
	wrapSock.observer_send = on_send;
	wrapSock.observer_send_exception = on_send_exception;

	{
		wrap_tcpsocket::write_scope ws(wrapSock);
		wrapSock.oarchive() << g_Texts << obj;
	}

	wrapSock.run();
	boost::system::error_code ec;
	ioserv.run();
	std::cout<<ec.message()<<std::endl;

	::system("Pause");
}